package com.supermate.supercut.infra.repoImpl;

import com.supermate.supercut.domain.constant.ResourceConst;
import com.supermate.supercut.domain.enums.ResourceTypeEnum;
import com.supermate.supercut.domain.model.ClipboardText;
import com.supermate.supercut.domain.model.FileResource;
import com.supermate.supercut.domain.repo.ClipboardResourceRepo;
import com.supermate.supercut.infra.convertor.ResourceConvertor;
import com.supermate.supercut.infra.dao.ResourceDAO;
import com.supermate.supercut.infra.entity.ResourceDO;
import com.supermate.supercut.infra.entity.ResourceParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 冰岩
 * @Date: 2023/10/28
 * @Description:
 */
@Component
public class ClipboardResourceRepoImpl implements ClipboardResourceRepo {

    @Autowired
    ResourceDAO resourceDAO;

    @Override
    public boolean saveOrUpdateClipboardText(ClipboardText text) {
        ResourceParam param = new ResourceParam();
        param.createCriteria().andClipNameEqualTo(text.getClipboardName());
        List<ResourceDO> resourceDOList = resourceDAO.selectByParam(param);
        ResourceDO resourceInDb = hasTextRecord(resourceDOList);
        text.setGmtModify(new Date());
        if (Objects.isNull(resourceInDb)) {
            text.setGmtCreate(new Date());//保存
            ResourceDO resourceDO = ResourceConvertor.toResourceDo(text);
            return resourceDAO.insert(resourceDO) > 0;
        }
        //更新
        resourceInDb.setContent(text.getClipboardText());
        resourceInDb.setGmtModify(text.getGmtModify());
        return resourceDAO.updateByPrimaryKey(resourceInDb) > 0;
    }

    private ResourceDO hasTextRecord(List<ResourceDO> resourceDOList) {
        return resourceDOList.stream()
                .filter(resourceDO -> Objects.equals(resourceDO.getResourceType(),
                        ResourceTypeEnum.TEXT.getCode()))
                .findFirst()
                .orElse(null);
    }

    @Override
    public boolean saveFileResource(FileResource fileResource) {
       ResourceDO resourceDO = ResourceConvertor.toResourceDo(fileResource);
       if (resourceDO.getGmtCreate() == null) {
           resourceDO.setGmtCreate(new Date());
       }
       if (resourceDO.getGmtModify() == null) {
           resourceDO.setGmtModify(new Date());
       }
       return resourceDAO.insertSelective(resourceDO) > 0;
    }

    @Override
    public ClipboardText queryClipboardText(String clipboardName) {
        ResourceParam textParam = new ResourceParam();
        ResourceParam.Criteria criteria = textParam.createCriteria();
        criteria.andClipNameEqualTo(clipboardName);
        criteria.andResourceTypeEqualTo(ResourceTypeEnum.TEXT.getCode());
        List<ResourceDO> resourceDOS = resourceDAO.selectByParam(textParam);
        if (CollectionUtils.isEmpty(resourceDOS)) {
            return null;
        }
        return ResourceConvertor.toClipboardText(resourceDOS.get(0));
    }

    @Override
    public List<FileResource> queryFileLists(String clipboardName) {
        ResourceParam fileParam = new ResourceParam();
        ResourceParam.Criteria criteria = fileParam.createCriteria();
        criteria.andClipNameEqualTo(clipboardName);
        criteria.andResourceTypeEqualTo(ResourceTypeEnum.FILE.getCode());
        criteria.andDeleteFlagNotEqualTo(ResourceConst.deleteFlag);
        List<ResourceDO> resourceDOS = resourceDAO.selectByParam(fileParam);
        return Optional.ofNullable(resourceDOS).orElse(Collections.emptyList())
            .stream().map(ResourceConvertor::toFileResource)
            .filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Override
    public boolean deleteFile(Long fileId) {
        return false;
    }

}
